package com.miroservice.chapter2.web;

import com.miroservice.chapter2.config.AppConfig;
import com.miroservice.chapter2.pojo.Singer;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.GenericApplicationContext;

import javax.validation.ConstraintViolation;
import java.util.Set;

public class JSR349Demo {


    public static void main(String[] args) {

        GenericApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);

        ValidateComponent validateController = ctx.getBean(ValidateComponent.class);

        Singer singer = new Singer();
        singer.setFistName("j");
        singer.setLastName("l");
        singer.setGenre(null);
        validatorSinger(singer, validateController);



    }


    private static void validatorSinger(Singer singer, ValidateComponent validateController) {

        final Set<ConstraintViolation<Singer>> constraintViolations = validateController.validatorSinger(singer);

        listVidlations(constraintViolations);

    }

    private static void listVidlations(Set<ConstraintViolation<Singer>> set) {

        for (ConstraintViolation<Singer> v : set) {
            System.out.print("property:" + v.getPropertyPath() + ",value:" + v.getInvalidValue() + ";error:" + v.getMessage());
        }

    }
}


